package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SearchSpecial;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 李海龙 on 15/10/27.
 */
public class SearchSpecialListViewAdapter extends BaseAdapter {
    private Context context;
    private List<SearchSpecial.DataEntity> dataEntities;

    public SearchSpecialListViewAdapter(Context context, List<SearchSpecial.DataEntity> dataEntities) {
        this.context = context;
        this.dataEntities = dataEntities;
    }

    public void loadData(List<SearchSpecial.DataEntity> dataEntities){
        this.dataEntities.addAll(dataEntities);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.size() : 0;
    }

    @Override
    public SearchSpecial.DataEntity getItem(int position) {
        return dataEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.search_specialalbum_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SearchSpecial.DataEntity dataEntity = getItem(position);
        holder.specialAlbumName.setText(dataEntity.getName());
        holder.specialAlbumSingerName.setText(dataEntity.getSingerName());
        holder.specialAlbumTime.setText(dataEntity.getPublishDate() + "");
        Picasso.with(context).load(dataEntity.getPicUrl()).placeholder(R.mipmap.img_music_default_icon)
                .error(R.mipmap.img_music_default_icon).into(holder.specialAlbumHead);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'search_specialalbum_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.special_album_head)
        ImageView specialAlbumHead;
        @Bind(R.id.special_albumName)
        TextView specialAlbumName;
        @Bind(R.id.special_album_singerName)
        TextView specialAlbumSingerName;
        @Bind(R.id.special_albumTime)
        TextView specialAlbumTime;
        @Bind(R.id.special_album_rightArr)
        ImageView specialAlbumRightArr;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
